package com.lxmes.business.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.lxmes.base.domain.JcIteminfo;
import com.lxmes.base.domain.JcIteminfoDistri;
import com.lxmes.base.mapper.JcIteminfoDistriMapper;
import com.lxmes.base.mapper.JcIteminfoMapper;
import com.lxmes.business.domain.CcStockInfo;
import com.lxmes.business.mapper.CcStockInfoMapper;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.business.mapper.PsYhPlanMapper;
import com.lxmes.business.domain.PsYhPlan;
import com.lxmes.business.service.IPsYhPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 要货计划Service业务层处理
 *
 * @author 张志煜
 * @date 2023-03-21
 */
@Service
public class PsYhPlanServiceImpl implements IPsYhPlanService
{
    @Autowired
    private PsYhPlanMapper psYhPlanMapper;
    @Autowired
    private JcIteminfoDistriMapper distriMapper;
    @Autowired
    private CcStockInfoMapper ccStockInfoMapper;
    @Autowired
    private JcIteminfoMapper iteminfoMapper;

    /**
     * 查询要货计划
     *
     * @param id 要货计划主键
     * @return 要货计划
     */
    @Override
    public PsYhPlan selectPsYhPlanById(Long id)
    {
        return psYhPlanMapper.selectPsYhPlanById(id);
    }

    /**
     * 查询要货计划列表
     *
     * @param psYhPlan 要货计划
     * @return 要货计划
     */
    @Override
    public List<PsYhPlan> selectPsYhPlanList(PsYhPlan psYhPlan)
    {
        return psYhPlanMapper.selectPsYhPlanList(psYhPlan);
    }

    @Override
    public List<PsYhPlan> getAddPsYhPlanList(PsYhPlan psYhPlan) {
        JcIteminfoDistri distri=new JcIteminfoDistri();
        distri.setStock1(psYhPlan.getKuquCode());
        distri.setFactoryCode(SecurityUtils.getFactoryCode());
        List<PsYhPlan> planList=new ArrayList<>();
        List<JcIteminfoDistri> list = distriMapper.selectJcIteminfoDistriList(distri);
        if(list.size()>0){
            for (JcIteminfoDistri dis:list){
                CcStockInfo ccStockInfo=new CcStockInfo();
                ccStockInfo.setItemId(dis.getItemId());
                ccStockInfo.setKuquCode(dis.getStock1());
                List<CcStockInfo> stockList = ccStockInfoMapper.selectCcStockInfoList3(ccStockInfo);
                if(stockList.size()>0){
                    for (CcStockInfo stock:stockList) {
                        //低于下限进行库存补货
                        if(stock.getKyNum().compareTo(new BigDecimal(dis.getStock1Xx()))<1){
                            PsYhPlan plan=new PsYhPlan();
                            BeanUtils.copyProperties(stock,plan);
                            plan.setYhDwCode(psYhPlan.getKuquCode());
                            plan.setYhDwName(psYhPlan.getKuquCode());
                            plan.setFactoryCode(dis.getFactoryCode());
                            plan.setFactoryName(dis.getFactoryName());
                            plan.setJhYhNum(new BigDecimal(dis.getStock1Sx()).subtract(stock.getKyNum()));
                            planList.add(plan);
                        }
                    }
                }else {
                    JcIteminfo info =iteminfoMapper.selectJcIteminfoByItemId(dis.getItemId());
                    PsYhPlan plan=new PsYhPlan();
                    BeanUtils.copyProperties(info,plan);
                    plan.setYhDwCode(psYhPlan.getKuquCode());
                    plan.setYhDwName(psYhPlan.getKuquCode());
                    plan.setFactoryCode(dis.getFactoryCode());
                    plan.setFactoryName(dis.getFactoryName());
                    plan.setJhYhNum(new BigDecimal(dis.getStock1Sx()));
                    planList.add(plan);

                }
            }
        }else {
            throw new ServiceException("查询物料储分配送工艺基础表无数据！");
        }
        return planList;
    }

    /**
     * 新增要货计划
     *
     * @param psYhPlan 要货计划
     * @return 结果
     */
    @Override
    public int insertPsYhPlan(PsYhPlan psYhPlan)
    {
        psYhPlan.setCreateTime(DateUtils.getNowDate());
        return psYhPlanMapper.insertPsYhPlan(psYhPlan);
    }

    @Override
    @Transactional
    public int insertPsYhPlanList(List<PsYhPlan> planList) {
        String planId= "PSYH"+ NewNumber.createNewNumber(psYhPlanMapper.getNewCode(DateUtils.getDate()));
       if(planList.size()>0){
           for (PsYhPlan plan:planList){
               plan.setYhPlanId(planId);
               plan.setYhType("安全库存");
               plan.setCreateTime(DateUtils.getNowDate());
               plan.setCreateBy(SecurityUtils.getUsername());
               plan.setYhDate(new Date());
               psYhPlanMapper.insertPsYhPlan(plan);
           }
       }else {
           throw new ServiceException("补货计划无数据，保存失败！");
       }
        return 1;
    }
    /**
     * 修改要货计划
     *
     * @param psYhPlan 要货计划
     * @return 结果
     */
    @Override
    public int updatePsYhPlan(PsYhPlan psYhPlan)
    {
        psYhPlan.setUpdateTime(DateUtils.getNowDate());
        return psYhPlanMapper.updatePsYhPlan(psYhPlan);
    }

    /**
     * 批量删除要货计划
     *
     * @param ids 需要删除的要货计划主键
     * @return 结果
     */
    @Override
    public int deletePsYhPlanByIds(Long[] ids)
    {
        return psYhPlanMapper.deletePsYhPlanByIds(ids);
    }

    /**
     * 删除要货计划信息
     *
     * @param id 要货计划主键
     * @return 结果
     */
    @Override
    public int deletePsYhPlanById(Long id)
    {
        return psYhPlanMapper.deletePsYhPlanById(id);
    }
}
